﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using KingSite.SyncDB.Core.Entity;
using System.Collections;

namespace KingSite.SyncDB.Core.Repository {
    public class SyncRepository : BaseProvider {
        /// <summary>
        /// 获得所有的同步任务
        /// </summary>
        /// <returns></returns>
        public IList<SyncJobInfo> GetAllSyncJob() {
            String stmtId = "SyncJobInfo.GetAllSyncJob";
            IList<SyncJobInfo> list = SqlMapper.QueryForList<SyncJobInfo>(stmtId, null);
            return list;
        }

        /// <summary>
        /// 获得任务实体，即每个任务的具体信息，那个数据库的那个表的哪些字段要同步到那里去
        /// </summary>
        /// <param name="jobInfo"></param>
        /// <returns></returns>
        public IList<SyncEntity> GetSyncEntityList(SyncJobInfo jobInfo) {
            String stmtId = "SyncEntity.GetSyncEntityList";
            Hashtable ht = new Hashtable();
            ht["sdbid"] = jobInfo.SdbId;
            ht["stid"] = jobInfo.StId;
            ht["ddbid"] = jobInfo.DdbId;
            ht["dtid"] = jobInfo.DtId;
            IList<SyncEntity> list = SqlMapper.QueryForList<SyncEntity>(stmtId, ht);
            return list;
        }

        public void InsertSyncJob(int sDbId, int dDbId, string sTableName, string dTableName, IList<ColumnEntity> sColumnEntityList, IList<ColumnEntity> dColumnEntityList, bool isSyncUpdate, bool isSyncInsert, string LastTimeStamp) {

            TBEntity stb = GetTableEntity(sDbId, sTableName);
            TBEntity dtb = GetTableEntity(dDbId, sTableName);
            stb.LastTimeStamp = LastTimeStamp;
            stb.IsSyncUpdate = isSyncUpdate;
            stb.IsSyncInsert = isSyncInsert;
            //插入源表，目标表
            TBRepository tbr = new TBRepository();
            if (stb.Id == 0) {
                tbr.InsertTBEntity(stb);
            }
            else {
                tbr.UpdateTBEntity(stb);
            }
            if (dtb.Id == 0) {
                tbr.InsertTBEntity(dtb);
            }
            else {
                tbr.UpdateTBEntity(dtb);
            }
            //插入表之后重新获得表对象
            stb = GetTableEntity(sDbId, sTableName);
            dtb = GetTableEntity(dDbId, sTableName);
            List<int> scidList = new List<int>();
            List<int> dcidList = new List<int>();
            //插入源列，目标列
            ColumnRepository cr = new ColumnRepository();
            foreach(ColumnEntity column in sColumnEntityList){
                column.DbId = sDbId;
                column.TableId = stb.Id;
                int id = cr.GetColumnId(stb.Id, column.ColumnName);
                column.Id = id;
                if (id <= 0) {
                    cr.InsertColumnEntity(column); //列不存在，插入
                    id = cr.GetColumnId(stb.Id, column.ColumnName);
                }
                else {
                    cr.UpdateColumnEntity(column);
                }
                scidList.Add(id);
            }
            foreach (ColumnEntity column in dColumnEntityList) {
                column.DbId = dDbId;
                column.TableId = dtb.Id;
                int id = cr.GetColumnId(dtb.Id, column.ColumnName);
                column.Id = id;
                if (id <= 0) {
                    cr.InsertColumnEntity(column);
                    id = cr.GetColumnId(dtb.Id, column.ColumnName);
                }
                else {
                    cr.UpdateColumnEntity(column);
                }
                dcidList.Add(id);
            }
            //插入同步列
            int i = 0;
            foreach (int scid in scidList) {
                int dcid = dcidList[i];
                if (GetSyncEntity(scid, dcid) <=0 ) {
                    InsertSyncEntity(scid, dcid);
                }
                i++;
            }
        }

        public void InsertSyncEntity(int scid, int dcid) {
            String stmtId = "SyncEntity.Insert";
            Hashtable ht = new Hashtable();
            ht.Add("SCId", scid);
            ht.Add("DCId", dcid);
            SqlMapper.Insert(stmtId, ht);
        }

        /// <summary>
        /// 清除同步任务
        /// </summary>
        /// <param name="dbId"></param>
        /// <param name="tableId"></param>
        public void ClearSync(int dbId, int tableId) {
            //先删除Sync表
            Delete(dbId, tableId);
            
            //ColumnRepository cr = new ColumnRepository();
            ////删除列记录
            //cr.DeleteFromTableId(tableId);

            //TBRepository tbr = new TBRepository();
            ////删除表记录
            //tbr.Delete(tableId);
        }

        public int GetSyncEntity(int scid, int dcid) {
            String stmtId = "SyncEntity.GetSyncEntity";
            Hashtable ht = new Hashtable();
            ht.Add("scid", scid);
            ht.Add("dcid", dcid);
            return SqlMapper.QueryForObject<int>(stmtId, ht);
        }

        public void Delete(int dbId, int tableId) {
            String stmtId = "SyncEntity.Delete";
            Hashtable ht = new Hashtable();
            ht.Add("DbId", dbId);
            ht.Add("TableId", tableId);
            SqlMapper.Delete(stmtId, ht);
        }

        public void Delete(int syncId) {
            String stmtId = "SyncEntity.DeleteBySyncId";
            SqlMapper.Delete(stmtId, syncId);
        }

        private TBEntity GetTableEntity(int dbid, string tableName) {
            TBRepository tr = new TBRepository();
            TBEntity entity = tr.GetTBEntity(dbid, tableName);
            if (entity == null) {
                entity = new TBEntity();
                entity.DbId = dbid;
                entity.TableName = tableName;
                entity.LastTimeStamp = "1";
                entity.IsSyncUpdate = true;
                entity.IsSyncInsert = true;
                entity.SyncLevel = 1;
            }
            return entity;
        }
     }
}
